﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DevExpress.Web;
using DevExpress.Web.ASPxTreeView;

public partial class UC_TreeSelect : System.Web.UI.UserControl
{
    public string KeyValue
    {
        get { return DropDownTree.KeyValue.ToString(); }
        set { DropDownTree.KeyValue = value; }
    }

    public string Text
    {
        get { return DropDownTree.Text; }
        set { DropDownTree.Text = value; }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            CreatTree();
        }
    }

    void CreatTree()
    {
        using (DAL.DataClassesDataContext dc = new DAL.DataClassesDataContext())
        {
            ASPxTreeView tree = DropDownTree.FindControl("Tree") as ASPxTreeView;

            var pnodes = dc.css_department.Where(d => d.lid == 1);
            foreach (var item in pnodes)
            {
                TreeViewNode pnode = new TreeViewNode(item.bmmc, item.bmbm, "../Images/icon/Depatment.png");
                CreatChildTree(pnode);
                tree.Nodes.Add(pnode);
            }

            tree.ExpandAll();
        }
    }

    void CreatChildTree(TreeViewNode pnode)
    {
        using (DAL.DataClassesDataContext dc = new DAL.DataClassesDataContext())
        {
            var snodes = dc.css_department.Where(sd => sd.fbmbm == pnode.Name);
            if (snodes.Count() > 0)
            {
                foreach (var sitem in snodes)
                {
                    TreeViewNode snode = new TreeViewNode(sitem.bmmc, sitem.bmbm, "../Images/icon/Door.png");
                    var ssnodes = dc.css_department.Where(ssd => ssd.fbmbm == sitem.bmbm);
                    //递归实现无线构造
                    CreatChildTree(snode);
                    pnode.Nodes.Add(snode);
                }
            }
        }
    }
}